/////////////////////////////////////////////////////////////////////////////
//
//  Fifteen Puzzle
//  Kolonitsky Developers Alliance (c) 2012
//  All rights received
//
/////////////////////////////////////////////////////////////////////////////

package view
{

    import flash.display.Sprite;

    import model.GameProxy;
    import model.vo.PuzzleVO;

    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    import view.events.GameEvent;

    public class GameMediator extends Mediator
    {
        public static const NAME:String = "GameMediator";



        public function GameMediator(game:Sprite)
        {
            super(NAME, game);
        }

        public function get game():Game
        {
            return viewComponent as Game;
        }




        //--------------------------------------------------------------------
        //
        // Mediator overriden methods
        //
        //--------------------------------------------------------------------

        override public function onRegister():void
        {
            super.onRegister();
            game.addEventListener(GameEvent.CREATE, game_createHandler);
            game.addEventListener(GameEvent.START, game_startHandler);
            game.addEventListener(GameEvent.RESULT, game_resultHandler);
            game.addEventListener(GameEvent.MOVE, game_moveHandler);
            game.addEventListener(GameEvent.MENU, game_menuHandler);
            sendNotification(AppFacade.MENU_GAME);
        }

        override public function onRemove():void
        {
            super.onRemove();
            game.removeEventListener(GameEvent.CREATE, game_createHandler);
            game.removeEventListener(GameEvent.START, game_startHandler);
            game.removeEventListener(GameEvent.RESULT, game_resultHandler);
            game.removeEventListener(GameEvent.MOVE, game_moveHandler);
            game.removeEventListener(GameEvent.MENU, game_menuHandler);
        }

        override public function listNotificationInterests():Array
        {
            return [
                GameProxy.CHANGED
            ]
        }

        override public function handleNotification(notification:INotification):void
        {
            switch (notification.getName())
            {
                case GameProxy.CHANGED:
                {
                    game.updateGame(notification.getBody() as PuzzleVO);
                    break;
                }
            }
        }




        //--------------------------------------------------------------------
        //
        // Private logic
        //
        //--------------------------------------------------------------------

        private function game_createHandler(event:GameEvent):void
        {
            sendNotification(AppFacade.CREATE_GAME);
        }

        private function game_startHandler(event:GameEvent):void
        {
            sendNotification(AppFacade.PLAY_GAME)
        }

        private function game_resultHandler(event:GameEvent):void
        {
            sendNotification(AppFacade.GAME_RESULT)
        }

        private function game_moveHandler(event:GameEvent):void
        {
            sendNotification(AppFacade.MOVE, null, event.direction);
        }

        private function game_menuHandler(event:GameEvent):void
        {
            sendNotification(AppFacade.MENU_GAME);
        }

    }
}
